package jiuqi.controller;

import com.github.pagehelper.PageInfo;
import jiuqi.pojo.User;
import jiuqi.pojo.query.UserQuery;
import jiuqi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    //显示
    @GetMapping("/homePage")
    public String index(Model model, UserQuery userQuery) {
        PageInfo<User> userPageInfo = userService.listUserByName(userQuery);
        model.addAttribute("page", userPageInfo);
        return "crud/content";
    }

    @PostMapping("/homePage")
    public String listUserByName(Model model, UserQuery userQuery) {
        PageInfo<User> userPageInfo = userService.listUserByName(userQuery);
        model.addAttribute("page", userPageInfo);
        return "crud/content";
    }

    //删除
    @GetMapping("/delete/{id}")
    public String deleteUserById(@PathVariable("id") Integer id, RedirectAttributes attributes) {
        Boolean flag = userService.deleteUserById(id);
        if (flag) {
            attributes.addFlashAttribute("message", "删除用户成功");
            return "redirect:/homePage";
        } else {
            attributes.addFlashAttribute("message", "删除用户失败");
            return "redirect:/homePage";
        }
    }

    //修改
    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("user", userService.queryUserById(id));
        return "crud/editUser";
    }

    @PostMapping("/edit")
    public String editUser(User user, RedirectAttributes attributes, UserQuery userQuery) {
        //判断修改的用户名是否已存在
        user.setName(user.getName());
        PageInfo<User> userPageInfo = userService.listUserByNameOne(userQuery);
        System.out.println("userPageInfo.getSize()=>" + userPageInfo.getSize());

        if (userPageInfo.getSize() <= 1) {
            boolean flag = userService.updateUser(user);
            if (flag) {
                attributes.addFlashAttribute("message", "修改用户成功");
                return "redirect:/homePage";
            } else {
                attributes.addFlashAttribute("message", "修改用户失败");
                return "redirect:/homePage";
            }
        } else {
            attributes.addFlashAttribute("message", "用户名 [ " + user.getName() + " ] 已存在");
            return "redirect:/edit/" + user.getId();
        }
    }

    //添加
    @GetMapping("/update")
    public String toUpdate(Model model) {
        User user = new User();
        model.addAttribute("user", user);
        return "crud/addUser";
    }

    @PostMapping("/add")
    public String addUser(User user, RedirectAttributes attributes, UserQuery userQuery) {
        //判断修改的用户名是否已存在
        user.setName(user.getName());
        PageInfo<User> userPageInfo = userService.listUserByNameOne(userQuery);
        System.out.println("userPageInfo.getSize()=>" + userPageInfo.getSize());

        if (userPageInfo.getSize() <= 1) {
            boolean flag = userService.addUser(user);
            if (flag) {
                attributes.addFlashAttribute("message", "新增用户成功");
                return "redirect:/homePage";
            } else {
                attributes.addFlashAttribute("message", "新增用户失败");
                return "redirect:/homePage";
            }
        } else {
            attributes.addFlashAttribute("message", "用户名 [ " + user.getName() + " ] 已存在");
            return "redirect:/update";
        }

    }
}
